<script>
import { getIdleSpace } from "@/api/modules/home";

export default {
  data() {
    return {
      carList: [],
      contentRow: {},
    };
  },
  computed: {
    userInfo() {
      return this.$store.getters.userInfoGetter;
    },
  },
  mounted() {
    this.getInitFn();
    window.setPlateNumber = this.setPlateNumber;
    window.setPlateBase64 = this.setPlateBase64;
    window.goManually = this.goManually;
  },
  methods: {
    goManually() {
      this.$router.push({
        path: "/entryCar",
        query: {
          carNo: "",
          parkspaceCode: this.contentRow.parkspaceCode,
          roadCode: this.contentRow.roadCode,
        },
      });
    },
    setPlateBase64(url) {
      this.$store.commit("setPlateImg", url);
    },
    onClickLeft() {
      this.$router.go(-1);
    },
    setPlateNumber(carNo) {
      this.$router.push({
        path: "/carForm",
        query: {
          carNo: carNo,
          parkspaceCode: this.contentRow.parkspaceCode,
          roadCode: this.contentRow.roadCode,
        },
      });
    },
    scanCode() {
      this.$store.commit("setPlateImg", "");
      try {
        // eslint-disable-next-line no-undef
        window.android.goCameraScanActivity();
      } catch (e) {
        this.$router.push({
          path: "/entryCar",
          query: {
            parkspaceCode: this.contentRow.parkspaceCode,
            roadCode: this.contentRow.roadCode,
          },
        });
      }
    },
    onClickCar(row) {
      this.contentRow = row;
      this.scanCode();
      // this.$router.push({
      //   path: "/entryCar",
      //   query: {
      //     parkspaceCode: row.parkspaceCode,
      //     roadCode: row.roadCode,
      //   },
      // });
    },
    async getInitFn() {
      const data = await getIdleSpace({
        workstationId: this.userInfo.workstationId,
        tollmanId: this.userInfo.id,
      });

      if (data.code === 200) {
        data.data.forEach((item) => {
          item.parkspaceList.forEach((it) => {
            this.carList.push({
              parkspaceCode: it,
              carStatus: 0,
              roadCode: item.roadCode,
            });
          });
        });
      }
    },
  },
};
</script>
<template>
  <div class="carParking">
    <div class="carParkingHead">
      <van-nav-bar title="选择入车车位">
        <template #left>
          <span
            style="font-size: 30px; background-color: #06c68a; padding: 4px 0"
            @click="$router.go(-1)"
            class="tc ting-fanhui"
            >返回
          </span>
        </template>
      </van-nav-bar>
    </div>
    <div class="carParking-content">
      <div
        @click="onClickCar(item)"
        class="carParking-content-items"
        v-for="item in carList"
        :key="item.parkspaceCode"
      >
        <i class="tc ting-dingwei"></i>
        <span>{{ item.parkspaceCode }}</span>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.carParking {
  width: 100%;

  .carParkingHead {
    width: 100%;
    position: fixed;
    top: 0;
    z-index: 999;
    box-shadow: 0 8px 20px 0 rgba(0, 0, 0, 0.1);

    i {
      font-size: 20px;
    }
  }

  .carParking-content {
    width: 100%;
    margin-top: 48px;
    height: calc(100% - 48px);
    overflow: auto;
    padding: 12px;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;

    .carParking-content-items {
      width: 46%;
      height: 50px;
      background: white;
      margin-bottom: 15px;
      border-radius: 10px;
      box-shadow: 0 8px 20px 0 rgba(0, 0, 0, 0.1);
      padding: 10px;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      justify-content: center;

      i {
        font-size: 20px;
      }

      span {
        font-size: 20px;
        margin-left: 10px;
        margin-right: 15px;
      }
    }
  }
}
</style>
